<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章</el-breadcrumb-item>
      <el-breadcrumb-item>文章分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 --> 
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
          <el-col :span="8">
            <el-input placeholder="请输入内容" v-model="queryInfo.keywords"
            clearable
            @clear="getCateList">
            <el-button
            slot="append"
            icon="el-icon-search"
            @click="getCateList"
            clearable></el-button>
            </el-input>
          </el-col>
          <el-col :span="4">
              <el-button type="primary" @click="formDialog('add')">添加分类</el-button>
          </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="cateList" border stripe="stripe">
          <el-table-column type="index" label="#"></el-table-column>
          <el-table-column label="分类名称" prop="name"></el-table-column>
          <el-table-column label="分类别名" prop="alias"></el-table-column>
          <el-table-column label="操作" min-width="180px">
              <template #default="{row}">
                  <!-- 修改按钮 -->
                  <el-button type="primary" icon="el-icon-edit" size="mini"
                  @click="formDialog('edit', row)"></el-button>
                  <!-- 删除按钮 -->
                  <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeCate(row.id)"></el-button>
              </template>
          </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.page"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加文章分类的对话框 -->
    <el-dialog
    :title="dialogTitle"
    :visible.sync="dialog"
    width="50%"
    @close="$refs.formRef.resetFields()">
      <!-- 内容主体区域 -->
      <el-form :model="form" :rules="formRules" ref="formRef" label-width="80px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="alias">
          <el-input v-model="form.alias"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary" @click="savaCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { cate } from '@/api/article.js';
export default {
  data () {
    return {
      cateList: [],
      // 获取用户列表的参数对象
      queryInfo: {
        keywords: '',
        // 当前的页数
        page: 1,
        // 当前每页显示多少条数据
        pagesize: 2
      },
      total: 0,
      dialogTitle: '添加文章分类',
      //   控制用户对话框的显示与隐藏
      dialog: false,
      //   添加用户的表单数据
      form: {
        name: '',
        alias: '',
      },
      //   添加表单的验证规则对象
      formRules: {
        name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符之间', trigger: 'blur' }
        ],
        alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符之间', trigger: 'blur' }
        ],
      },
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    async getCateList () {
      console.log(this.queryInfo);
        const { data: res } = await cate.list(this.queryInfo)
        if (res.status !== 0) return this.$message.error(res.message)
        console.log(res);
        this.cateList = res.data
        this.total = res.total
    },
    // 对话框
    formDialog (type = 'add', info = {}) {
      console.log(info);
      this.dialogTitle = type === 'add' ? '添加文章分类' : '更新文章分类';
      this.form = info
      // 存储信息
      sessionStorage.setItem('cateInfo', JSON.stringify(this.form))
      this.dialog = true
    },
    // 监听 pagesize 改变的事件
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange (newPage) {
      this.queryInfo.page = newPage
      this.getCateList()
    },
    // 保存
    async savaCate () {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        // 添加 or 修改
        if (this.dialogTitle === '添加文章分类') {
          const { data: res } = await cate.add(this.form)
          console.log(res);
          if (res.status !== 0)return this.$message.error(res.message)
          this.$message.success('添加文章分类成功')
        }
        if (this.dialogTitle === '更新文章分类') {
          if (JSON.stringify(this.form) === sessionStorage.getItem('cateInfo')) return (this.dialog = false)
          const { data: res } = await cate.update(this.form)
          // console.log(res);
          if (res.status !== 0)return this.$message.error(res.message)
          this.$message.success('更新文章分类成功')
        }
        this.dialog = false
        this.getCateList()
      })
    },
    // 根据id删除对应的用户信息
    async removeCate (id) {
      // 弹框询问用户是否删除数据
      const confirmRsults = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 确认删除 confirm；取消删除 cancel
      if (confirmRsults !== 'confirm') return this.$message.info('已取消删除')

      // console.log('确认删除')
      const { data: res } = await cate.delete(id)
      console.log(res);
      if (res.status !== 0) return this.$message.error('删除分类失败')
      this.$message.success('删除分类成功')
      this.getCateList()
    },
  }
}
</script>

<style scoped lang="less">

</style>
